var $util = require('../util/util');
var $sql = require('../dao/filedSqlMapping');
var qr = require('qr-image');
var mq = require('mysql-queries');

module.exports = {
	// 球场列表
	selectField : function(req,res,next){
		mq.query($sql.filedsList,function(err,result){
			$util.jsonWrite(res, result);
		});
	},
	// 球场详情
	fieldDetail:function(req,res,next){
		var param = req.body;
		mq.query($sql.fieldDetail,[param.id],function(err,result){
			$util.jsonWrite(res, result);	
		});
	},
	//获取场地配置参数
	fieldConfigs:function(req,res,next){
		var datajson = {};
		var params = req.body;
		mq.queries([$sql.fieldRules,$sql.fields,$sql.fieldNums,$sql.fieldRanges,$sql.daysLimit],
			[[],[params.fieldId],[params.fieldId],[],['days-limit']],
			function(err,result){
				datajson.fieldRules = result[0] || [];
				datajson.fields = result[1] || [];
				datajson.fieldNums = result[2]?result[2][0]:null;
				datajson.fieldRanges = result[3] || [];
				datajson.daysLimit = result[4] ? result[4][0].setting_value : 7;
				$util.jsonWrite(res, datajson);	
			}
		);
	},
	// 预定列表
	fieldReserve:function(req,res,next){
		var params = req.body;
		mq.query($sql.fieldOrders,[params.date],
			function(err,result){
				$util.jsonWrite(res, result);	
			}
		);
	},
	// 我的订单
	fieldOrder:function(req,res,next){
		var pagesize = req.body.pagesize;
		var page = req.body.page - 1;
		var pageindex = pagesize * req.body.page;
		var type = req.body.status;
		var userId = req.body.userId;
		var dateBegin = req.body.dateBegin,dateEnd = req.body.dateEnd;
		var sql = $sql.fieldOrder;
		if(dateBegin){
			sql += ' and rental_date>=? ';
		}
		if(dateEnd){
			sql += ' and rental_date<? ';
		}

		mq.query(sql,[userId,type,dateBegin,dateEnd],function(err,result){
			// console.log($sql.fieldOrder);
			// console.log(err);
			$util.jsonWrite(res, result);	
		});
	},
	paySuccess:function(out_trade_no,callback){
		mq.query($sql.paySuccess,[out_trade_no],function(err,result){
			callback();
		});
	},
	// 全部订单
	allFieldOrder:function(req,res,next){
		mq.query($sql.allFieldOrder,[req.body.userId],function(err,result){
			// console.log($sql.fieldOrder);
			// console.log(result);
			$util.jsonWrite(res, result);	
		});
	},
	deleteOrder:function(req,res,next){
		mq.query($sql.deleteOrder,[req.body.id],function(err,result){
			$util.jsonWrite(res, {status:200});	
		});
	},
	// 生成订单
	createOrder:function(req,res,next){
		var date = new Date();
		var dateTime = date.toLocaleDateString();
		var datetimes = date.toLocaleString();
		var datestr = dateTime.replace(/-/g,"");
		var timestamp = String(date.getTime());
		var randomstr = timestamp.substr(timestamp.length-6);
		var orderId = 'XSD' + datestr + randomstr;
		var params = req.body.data;
		mq.query($sql.checkFieldReserved,[params.date,params.field.id,params.range.id],function(err,result){
			if(result&&result[0].num>0){
				$util.jsonWrite(res,{orderId:null});
			}else{
				mq.query($sql.createOrder,[orderId,params.field.id,datetimes,params.date,params.price,0,params.userId,params.username,params.user_tel,params.range.start_time,params.range.end_time,0,params.invoice_title,params.invoice_item,params.range.id],function(err,result){
					if(result){
						result = {
							status:200,
							orderId:orderId
						}
					}
					$util.jsonWrite(res,result);
				})		
			}
		});
	},

	// 二维码生成
	create_qrcode:function(req,res,next){
		var text = req.query.text;
	    try {
	        var img = qr.image(text,{size :10});
	        res.writeHead(200, {'Content-Type': 'image/png'});
	        img.pipe(res);
	    } catch (e) {
	        res.writeHead(414, {'Content-Type': 'text/html'});
	        res.end('<h1>414 Request-URI Too Large</h1>');
	    }
	},
}